package com.lonty.vote.controller;

import com.lonty.vote.api.Result;
import com.lonty.vote.request.VoteList;
import com.lonty.vote.service.VoteRecordsService;
import com.lonty.vote.util.ExcelUtil;
import com.lonty.vote.vo.VoteRecordsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/5/25 10:16
 * @Version 1.0
 */
@RestController
@RequestMapping("app/voteRecords")
@Validated
@Api(tags = "投票记录接口", produces = "application/json")
public class VoteRecordsController {

    @Resource
    private VoteRecordsService voteRecordsService;

    @ApiOperation(value = "投票", httpMethod = "POST")
    @PostMapping("vote")
    public Result vote(VoteRecordsVo voteRecordsVo){
        return voteRecordsService.vote(voteRecordsVo);
    }

    @ApiOperation(value = "统计当天票数", httpMethod = "GET")
    @GetMapping("getVoteTodayList")
    public Result getVoteTodayList(){
        return Result.builder().data(voteRecordsService.getVoteTodayList()).build();
    }

    @ApiOperation(value = "统计所有投票总数", httpMethod = "GET")
    @GetMapping("getAllList")
    public Result getAllList(){
        return Result.builder()
                .data(voteRecordsService.getAllList())
                .build();
    }

    @ApiOperation(value = "导出excel", httpMethod = "GET")
    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        List<VoteList> excelData = voteRecordsService.getAllList();
        ExcelUtil.exportExcel(response, excelData, "投票统计表","投票统计表",15);
    }

}
